package com.itheima.controller;


import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.itheima.common.constant.RedisConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.config.AliOssProperties;
import com.itheima.pojo.Setmeal;
import com.itheima.service.ISetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-12-19
 */
@RestController
@RequestMapping("/setmeal")
@Api(tags = "套餐管理")
@Slf4j
public class SetmealController {
    @Autowired
    private ISetmealService setmealService;
    @Autowired
    private AliOssProperties aliOSSUtils;
    @Autowired
    private RedisTemplate redisTemplate;
    @PostMapping("/findPage")
    @ApiOperation("分页查询")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        log.info("分页查询:{}",queryPageBean);
        PageResult pageResult = setmealService.findPage(queryPageBean);
        return pageResult;
    }
    @GetMapping("/findById")
    @ApiOperation("根据id查询套餐")
    public Result findById(@RequestParam("id") Integer id){
        Setmeal setmeal = setmealService.findById(id);
        return new Result(true,"查询成功",setmeal);
    }
    @PostMapping("/add")
    @ApiOperation("新增套餐")
    public Result add(@RequestBody Setmeal setmeal, @RequestParam("checkgroupIds")List<Integer> checkGroupIds){
        log.info("新增套餐:{}",setmeal,checkGroupIds);
        setmealService.add(setmeal,checkGroupIds);
        return new Result(true,"新增成功");
    }

    @PostMapping("/upload")
    @ApiOperation("上传图片")
    public Result upload(MultipartFile imgFile) throws IOException {
        try {
            //原始文件名
            String originalFilename = imgFile.getOriginalFilename();
            //截取原始文件名的后缀
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            String objectName = UUID.randomUUID().toString() + extension;
            //上传到阿里云oss
            String filePath = aliOSSUtils.upload(imgFile.getBytes(), objectName);
            log.info("将文件传入至redis缓存当中:{}", filePath);
            redisTemplate.boundSetOps(RedisConstant.SETMEAL_PIC_RESOURCES).add(filePath);
            return new Result(true, "上传成功", filePath);
        } catch (IOException e) {
            log.error("文件上传失败:{}", e);
            return new Result(false, "上传失败");
        }
    }
    @PostMapping("/edit")
    @ApiOperation("修改套餐")
    public Result edit(@RequestBody Setmeal setmeal, @RequestParam("checkGroupIds")List<Integer> checkGroupIds){
        log.info("修改套餐:{}",setmeal,checkGroupIds);
        setmealService.edit(setmeal,checkGroupIds);
        return new Result(true,"修改成功");
    }
    @GetMapping("/deleteById")
    @ApiOperation("删除套餐")
    public Result deleteById(@RequestParam("id") Integer id){
        log.info("根据id删除套餐:{}",id);
        setmealService.deleteById(id);
        return new Result(true,"删除成功");
    }
    @GetMapping("/free")
    public void free() {
        setmealService.generateFile("setmeal");
    }

}
